<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">围栏管理</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="router.push('/geofence')">返回</el-button>
  </div>

  <div id="mapContainer" style="width: 100%;height: 84vh;"></div>

  <el-dialog title="围栏信息" v-model="dialogVisible">
    <el-form label-width="80px">
      <el-form-item label="围栏名称">
        <el-input placeholder="请输入围栏名称" v-model="geofence.name"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="addGeofence">确定</el-button>
    </template>
  </el-dialog>




</template>
<script setup>
import router from "@/router";
import {nextTick, onMounted, ref} from "vue";
import { DrawScene, DrawControl , OperateEventType} from 'bmap-draw';
import * as events from "events";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";

const dialogVisible = ref(false)

// 准备一个电子围栏对象 用于给后端发送新增请求时传递参数
const geofence = ref({
  name:'',
  position:{} // 这是一个对象
})
let map;
onMounted(()=>{
  nextTick(()=>{
    // 创建地图实例
    map = new BMapGL.Map("mapContainer");
    // 中心点
    let point = new BMapGL.Point(116.4074,39.9024);
    // 设置中心点和缩放级别 越大越细致
    map.centerAndZoom(point,15);
    // 添加鼠标绘制控件
    // 5.1 创建绘制场景类    这是其他绘制控件的基类，负责获取覆盖物 事件监听等 注意 需要导入
    const scene = new DrawScene(map);
    const drawContrl = new DrawControl(scene, {
      // 绘制控件自己的配置项: 不显示图标提示 显示在容器左上角， 显示圆形 与 矩形绘制按钮
      enableTips: false,
      anchor: BMAP_ANCHOR_TOP_RIGHT,
      drawingItems: [
        'circle',
        'rectangle'
      ]
    });
    // 监听绘制完成事件，获取触发完成事件的电子围栏目标对象，注意需要导入OperationEventType
    scene.addEventListener(OperateEventType.COMPLETE,(event)=>{
      dialogVisible.value = true  ;
      // event 是完成事件的返回值，DOM中所有事件对象都基于Event对象
      // event.target 用于获取当前出发完成事件的围栏元素
      console.log(event.target)
      let overlay = event.target.overlay;
      if (overlay instanceof BMapGL.Circle)
      {
        geofence.value.position.type='circle';
        geofence.value.position.longitude = overlay.getCenter().lng;
        geofence.value.position.latitude = overlay.getCenter().lat;
        geofence.value.position.radius= overlay.getRadius();
        console.log(geofence.value)
      }else {
        geofence.value.position.type='retangle';
        let recPoints = [] ;
        for (let i = 0 ; i < 4; i++)
        {
          recPoints.push(overlay.points[i].latLng.lng+'-'+overlay.points[i].latLng.lat);
        }
        // join 方法 将数组的内容以，为分割连接成字符串
        geofence.value.position.recPoints = recPoints.join(",")
        console.log(geofence.value)
      }


    })
    // 监听绘制完成事件 获取出发完成事件的电子围栏目标对象
    // 将准备好的绘制控件添加到地图上
    map.addControl(drawContrl);
  })
})

const closeDialog = ()=>
{
  if (confirm("are you 确定？"))
  {
    map.clearOverlays();
    geofence.value={name: '',position: {}}
    dialogVisible.value=false;
  }

}

const addGeofence = ()=>{
  if (!geofence.value.name || geofence.value.name.trim()=='')
  {
    ElMessage.error("请输入围栏名称")
    return;
  }
  // 由于geofence.value.position 是一个对象类型 需要将它先转为字符串
  console.log(geofence.value)
  geofence.value.position = JSON.stringify(geofence.value.position)
  console.log(geofence.value)
  let data = qs.stringify(geofence.value)
  axios.post(BASE_URL+"/v1/geofence/save",data).then(
      (response)=>{
        if (response.data.code == 2000)
        {
          ElMessage.success("保存围栏信息成功")
          router.push("/geofence")
        }else{
          ElMessage.error(response.data.msg)
        }
      }
  )
}
</script>
<style scoped>
</style>